package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

var UrlContainer chan Url
var PageContainer chan *Page
var ImageContainer chan *Image
var group sync.WaitGroup

func main() {
	fmt.Println("start crawling......")
	fmt.Println("creating Containers")
	UrlContainer = make(chan Url, 1000)
	PageContainer = make(chan *Page, 1000)
	ImageContainer = make(chan *Image, 1000)

	rootUrl := []string{"https://cn.bing.com/images/search?q=%E5%9B%BE%E7%89%87&form=HDRSC2&first=1&cw=1217&ch=606"}

	ctx, _ := context.WithTimeout(context.Background(), 1*time.Hour)

	fmt.Println("creating Managers")
	UrlDealerManager := NewUrlDealerManager()
	PageDealerManager := NewPageDealerManager()
	ImgDealerManager := NewImgDealerManager()

	fmt.Println("initializing Managers")
	UrlDealerManager.InitManager(5, ctx, &group, UrlDealer{})
	PageDealerManager.InitManager(5, ctx, &group, PageDealer{})
	ImgDealerManager.InitManager(5, ctx, &group, ImageDealer{})
	ImgDealerManager.SetFolder("D:/Learn_go/job1/photo/")

	fmt.Println("starting Managers")
	UrlDealerManager.Run()
	PageDealerManager.Run()
	ImgDealerManager.Run()

	fmt.Println("Process start")
	ok := StartWithRootUrl(rootUrl)
	if !ok {
		fmt.Println("process start failed")
		return
	}
	fmt.Println("process start success")

	group.Wait()
	fmt.Println("Process Ended")
}

func StartWithRootUrl(urls []string) bool {
	if len(urls) == 0 {
		return false
	}

	for _, url := range urls {
		UrlContainer <- Url(url)
	}
	return true
}
